package com.tdkj.action.front.information;

import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.information.BeanExpert;
import com.tdkj.service.information.IBeanExpertService;
public class ExpertAction  extends BaseAction{
	private Long id;
	private BeanExpert expert=new BeanExpert();
	private BeanBaseInfo article=new BeanBaseInfo();
	private IBeanExpertService expertService;
	private List<BeanExpert> experts=new ArrayList<BeanExpert>();
	private List<BeanBaseInfo> articles=new ArrayList<BeanBaseInfo>();
	private List<BeanBaseInfo> hotinfos;//热门信息
	private List<BeanBaseInfo> relevanceinfos=new ArrayList<BeanBaseInfo>();//相关信息
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public BeanExpert getExpert() {
		return expert;
	}
	public void setExpert(BeanExpert expert) {
		this.expert = expert;
	}
	public BeanBaseInfo getArticle() {
		return article;
	}
	public void setArticle(BeanBaseInfo article) {
		this.article = article;
	}
	public IBeanExpertService getExpertService() {
		return expertService;
	}
	@Resource
	public void setExpertService(IBeanExpertService expertService) {
		this.expertService = expertService;
	}
	public List<BeanExpert> getExperts() {
		return experts;
	}
	public void setExperts(List<BeanExpert> experts) {
		this.experts = experts;
	}
	public List<BeanBaseInfo> getArticles() {
		return articles;
	}
	public void setArticles(List<BeanBaseInfo> articles) {
		this.articles = articles;
	}
	public List<BeanBaseInfo> getHotinfos() {
		return hotinfos;
	}
	public void setHotinfos(List<BeanBaseInfo> hotinfos) {
		this.hotinfos = hotinfos;
	}
	public List<BeanBaseInfo> getRelevanceinfos() {
		return relevanceinfos;
	}
	public void setRelevanceinfos(List<BeanBaseInfo> relevanceinfos) {
		this.relevanceinfos = relevanceinfos;
	}
	
	public String findArticleContent(){
		expertService.findArticle(this.getId());
		return "articleContent";
	}
	
	public String findContent(){
		expert=expertService.findById(this.getId(), BeanExpert.class);
		//findOtherList();
		return "content";
	}
	
	//获取文章列表
	public String findArticles(){
		Criteria criteria = this.createCriteria(BeanBaseInfo.class);
		criteria.add(Restrictions.eq("moduletype", "11"));
		criteria.add(Restrictions.eq("columntype", "2"));
		criteria.addOrder(Order.desc("createtime"));// 对点击数进行排序
		this.queryByPage(criteria, "expertAction!findArticles.action", 10);
		return "articleList";
	}
	
	//获取专家列表
	public String findList(){
		Criteria criteria = this.createCriteria(BeanExpert.class);
		criteria.addOrder(Order.desc("experttype"));// 对状态进行排序
		this.queryByPage(criteria, "expertAction!findList.action", 10);
		//findList();
		return "list";
	}
	//获取专家栏目首页
	public String findAll(){
		experts=expertService.findAll(BeanExpert.class);
		if(experts.size()==0)appendObj(20);
		experts=expertService.findAll(BeanExpert.class);
		articles=expertService.findArticles();
//		Criteria criteria = this.createCriteria(BeanExpert.class);
//		criteria.addOrder(Order.desc("experttype"));// 对状态进行排序
//		this.queryByPage(criteria, "expertAction!findAll.action", 10);
//		findList();
		return "findAll";
	}
	
	private void findOtherList(){
		Criteria criteria2 = this.createCriteria(BeanBaseInfo.class);
		criteria2.add(Restrictions.eq("moduletype", "11"));
		criteria2.add(Restrictions.eq("columntype", "2"));
		criteria2.addOrder(Order.desc("clicktime"));// 对点击数进行排序
		hotinfos=this.getBaseService().findPageBy(criteria2, 1, 10).getList();
		
		Criteria criteria3 = this.createCriteria(BeanBaseInfo.class);
		criteria3.add(Restrictions.eq("moduletype", "11"));
		criteria3.add(Restrictions.eq("columntype", "2"));
		criteria3.addOrder(Order.desc("createtime"));// 对点击数进行排序
		relevanceinfos=this.getBaseService().findPageBy(criteria3, 1, 10).getList();
	}
	private  void appendObj(int num) {
		// TODO Auto-generated method stub
		BeanExpert bean=null;
		for(int i=0;i<num;i++){
			bean=new BeanExpert();
			bean.setExpertname("ename"+i);
			bean.setExperttype("t"+i);
			bean.setStatus("专家");
			bean.setDiscription("中国科学院院士");
			for(int j=0;j<10;j++){
				BeanBaseInfo article=new BeanBaseInfo();
				article.setTitle("文章");
				article.setContent("专家文章"+i);
				article.setUser(bean.getUser());
				article.setClicktime(new Random().nextInt(100));
				article.setCreatetime(new Timestamp(System.currentTimeMillis()));
				bean.getArticles().add(article);
			}
			expertService.save(bean);
		}
	}
	
}
